

//
//  LJHRecScrollView.m
//  HEARME
//
//  Created by liujinhui on 16/1/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHRecScrollView.h"
#import "LJHChildListModel.h"
#import "UIImageView+WebCache.h"
@interface LJHRecScrollView ()<UIScrollViewDelegate>

@property(nonatomic,retain)UIScrollView *headScrollView;
@property(nonatomic,retain)UIPageControl *pageC;

@end

@implementation LJHRecScrollView

- (void)dealloc
{
    [_headScrollView release];
    [_pageC release];
    
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createSubviews];
    }
    
    return self;
}

-(void)createSubviews
{
    
    self.headScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH*0.45)];
    [self addSubview:self.headScrollView];
    self.headScrollView.contentOffset = CGPointMake(WIDTH, 0);
    self.headScrollView.pagingEnabled = YES;
    self.headScrollView.showsHorizontalScrollIndicator = NO;
    self.headScrollView.delegate = self;
    [_headScrollView release];
    
    //设置page
    self.pageC = [[UIPageControl alloc]initWithFrame:CGRectMake(self.headScrollView.frame.size.width - self.headScrollView.frame.size.width/3 - self.headScrollView.frame.size.width/3, self.headScrollView.frame.size.height - self.headScrollView.frame.size.height/4, self.headScrollView.frame.size.width/3 - 20, self.headScrollView.frame.size.height/3)];
    self.pageC.backgroundColor = [UIColor clearColor];
    [self addSubview:self.pageC];
    //设置未选中点得颜色
    self.pageC.pageIndicatorTintColor = [UIColor lightGrayColor];
    //设置选中小点颜色
    self.pageC.currentPageIndicatorTintColor = [UIColor whiteColor];
    self.pageC.alpha = 0.6;
    
    self.pageC.currentPage = 0;
    
    
    
}
#pragma mark - 轮播图图片
-(void)setScrollViewImage:(NSMutableArray *)array
{
    
    if (array.count != 0) {
        NSLog(@"ddddd%lu",(unsigned long)array.count);
        NSMutableArray *imageArray = [NSMutableArray arrayWithObject:[array lastObject]];
        [imageArray addObjectsFromArray:array];
        [imageArray addObject:[array objectAtIndex:0]];
        
        self.headScrollView.contentSize = CGSizeMake(WIDTH*imageArray.count, WIDTH*0.45);
        for (int i = 0; i < imageArray.count; i++) {
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*WIDTH, 0, WIDTH, WIDTH*0.45)];
            LJHChildListModel *imageModel = [imageArray objectAtIndex:i];
            NSURL *url = [NSURL URLWithString:imageModel.pic];
            [imageView sd_setImageWithURL:url placeholderImage:PLACEHOLDER];
            [self.headScrollView addSubview:imageView];
            imageView.tag = i + 1000;
            [imageView release];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            
            imageView.userInteractionEnabled = YES;
            [imageView addGestureRecognizer:tap];
            
        }
        
    }
    self.pageC.numberOfPages = array.count;
    
    
    //计时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showAction) userInfo:nil repeats:YES];
    
    
}

#pragma mark - 点击轮播图（协议第二步）
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    
    [self.scrollDelegate sendMyImageTagBack:tap.view.tag-1000];
    
}

#pragma mark - 滑动停止时触发方法(轮播方法)
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    
    self.pageC.currentPage = scrollView.contentOffset.x/WIDTH-1;
    
    if (scrollView.contentOffset.x == (self.pageC.numberOfPages+1)*WIDTH) {
        
        [self.headScrollView setContentOffset:CGPointMake(WIDTH, 0)];
        self.pageC.currentPage = 0;
        
    }

    
}

#pragma mark - scrollView滑动方法

// 手指离开的时候开始滑动
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
//{
//    //    NSLog(@"11111111");
//    
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showAction) userInfo:nil repeats:YES];
////    if (self.headScrollView.contentOffset.x == (WIDTH * self.pageC.numberOfPages+1)){
////        //跳到第2张
////        self.headScrollView.contentOffset = CGPointMake(WIDTH, 0);
////        self.pageC.currentPage = 0;
////        
////    }
////    self.pageC.currentPage = self.headScrollView.contentOffset.x/WIDTH - 1;
//    
//}

// 手指接触的时候定时器停止
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//    
//    [self.timer invalidate];
//    self.timer = nil;
//    
//}



#pragma mark - 计时器方法
-(void)showAction{
    
//    NSLog(@"%f",self.headScrollView.contentOffset.x/WIDTH);
    
    
    if (self.headScrollView.contentOffset.x == WIDTH*(self.pageC.numberOfPages+1))
    {
        
        self.pageC.currentPage = 0;
        [self.headScrollView setContentOffset:CGPointMake(WIDTH, 0)];
        
        
        
    }
    [UIView animateWithDuration:1 animations:^{
//        [self.headScrollView setContentOffset:CGPointMake((self.headScrollView.contentOffset+WIDTH), 0) animated:YES];
        [self.headScrollView setContentOffset:CGPointMake((self.pageC.currentPage+1)*WIDTH + WIDTH, 0) animated:YES];
//        self.headScrollView.contentOffset = CGPointMake((self.headScrollView.contentOffset+WIDTH), 0);
        self.pageC.currentPage = self.headScrollView.contentOffset.x/WIDTH;
        
    }];
    
    
    

    
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
